package org.qiyu.live.im.router.provide.service.Impl;

import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.qiyu.live.im.constants.ImConstants;
import org.qiyu.live.im.core.server.interfaces.constants.ImCoreServicerConstants;
import org.qiyu.live.im.core.server.interfaces.rpc.ImRouterHandlerRpc;
import org.qiyu.live.im.dto.ImMsgBody;
import org.qiyu.live.im.router.provide.service.ImRouterService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2023-12-10 2:28
 */
@Service
public class ImRouterServiceImpl implements ImRouterService {
    @DubboReference
    private ImRouterHandlerRpc imRouterHandlerRpc;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean sendMsg(Long objectId, ImMsgBody imMsgBody) {
       //根据用户id获取绑定的ip
        String bindAddress = stringRedisTemplate.opsForValue().get(ImCoreServicerConstants.IM_BIND_IP_KEY + objectId + imMsgBody.getAppId());
        if(bindAddress==null){
            return false;
        }
        RpcContext.getContext().set("ip",bindAddress);
        //根据用户id发送
        imRouterHandlerRpc.sendMsg(imMsgBody);
        return false;
    }
}
